home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / hdf / unix / hdf3_2r2.lha / HDF3.2r2 / util / hdfls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-28  |  10.7 KB  |  380 lines

  1. /***************************************************************************
  2. *
  3. *
  4. *                         NCSA HDF version 3.2r2
  5. *                            October 30, 1992
  6. *
  7. * NCSA HDF Version 3.2 source code and documentation are in the public
  8. * domain.  Specifically, we give to the public domain all rights for future
  9. * licensing of the source code, all resale rights, and all publishing rights.
  10. *
  11. * We ask, but do not require, that the following message be included in all
  12. * derived works:
  13. *
  14. * Portions developed at the National Center for Supercomputing Applications at
  15. * the University of Illinois at Urbana-Champaign, in collaboration with the
  16. * Information Technology Institute of Singapore.
  17. *
  18. * THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE
  19. * SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION,
  20. * WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE
  21. *
  22. ****************************************************************************
  23. */
  24.  
  25.  
  26. #ifdef RCSID
  27. static char RcsId[] = "@(#)$Revision: 1.20 $";
  28. #endif
  29. /*
  30. $Header: /hdf/hdf/v3.2r2/util/RCS/hdfls.c,v 1.20 1992/10/12 18:33:28 koziol beta koziol $
  31.  
  32. $Log: hdfls.c,v $
  33.  * Revision 1.20  1992/10/12  18:33:28  koziol
  34.  * changed MAXBUFF to 8192
  35.  *
  36.  * Revision 1.19  1992/07/15  21:48:48  sxu
  37.  * No change
  38.  *
  39.  * Revision 1.18  1992/07/09  01:18:57  koziol
  40.  * More PC fixes
  41.  *
  42.  * Revision 1.17  1992/07/08  15:39:42  chouck
  43.  * Removed return(0) at end of main().  Took out some debugging
  44.  * info from the -t option
  45.  *
  46.  * Revision 1.16  1992/06/19  16:45:58  chouck
  47.  * More descriptive error messages when Hopen() fails
  48.  *
  49.  * Revision 1.15  1992/06/08  22:25:22  chouck
  50.  * Minor fix with program name
  51.  *
  52.  * Revision 1.14  1992/06/08  21:59:41  chouck
  53.  * Added 'verbose' option for labels/descriptions (-v) and
  54.  * option (-t #) to only list info about a given tag
  55.  *
  56.  * Revision 1.13  1992/05/31  19:16:26  mfolk
  57.  * No change.  But Convex doesn't like match of arg types in call to
  58.  * qsort on line 220.
  59.  *
  60.  * Revision 1.12  1992/05/26  21:00:08  koziol
  61.  * Folded Jason's Mac port and Linted code into the main version
  62.  *
  63.  * Revision 1.11  1992/04/24  15:38:25  koziol
  64.  * PC port
  65.  *
  66.  * Revision 1.10  1992/03/27  15:39:28  chouck
  67.  * Can now handle multiple command line options
  68.  *
  69.  * Revision 1.9  1992/03/25  22:51:35  chouck
  70.  * Fixed stupid bugs relating to unknown tags
  71.  *
  72.  * Revision 1.8  1992/03/23  17:21:52  likkai
  73.  * Put in description of "-d" option in "usage" message.
  74.  * and redo the format for the output of that option.
  75.  *
  76.  * Revision 1.7  1992/03/18  17:49:26  chouck
  77.  * Added Hendaccess() call so that Hclose() will not FAIL
  78.  * ,
  79.  *
  80.  * Revision 1.6  1992/03/11  20:53:33  chouck
  81.  * Use HDgettagname() to look up tag names.  Change the tag/ref look
  82.  * up routines to use Hiquire() and Hnextread instead of DFdescriptors()
  83.  *
  84.  * Revision 1.5  1992/02/29  18:58:36  sxu
  85.  * add header
  86.  *
  87.  * Revision 1.4  1992/02/26  17:46:31  likkai
  88.  * added descriptions for Vset elements.
  89.  *
  90.  * Revision 1.3  1992/02/21  21:06:29  mfolk
  91.  * no change
  92.  *
  93.  * Revision 1.2  1992/02/11  17:16:24  chouck
  94.  * Cosmetic changes.
  95.  * Added new tag values
  96.  *
  97.  * Revision 3.3  1991/10/22  17:56:10  dilg
  98.  * 5
  99.  * HDF3.1r5
  100.  *
  101.  * New machine types added:
  102.  *
  103.  *         PC      - IBM PC (DOS)
  104.  *         WIN     - IBM PC (Microsoft Windows 3.0)
  105.  *         IBM6000 - IBM RS/6000 (AIX)
  106.  *         CONVEX  - Convex C-2 (Unix)
  107.  *
  108.  * Bugs fixed in:
  109.  *
  110.  *         scup32.f
  111.  *         cspck32.f
  112.  *         dfpFf.f
  113.  *         dfpF.c
  114.  *         dfsd.c
  115.  *
  116.  * New utility added:
  117.  *
  118.  *         ristosds.c - convert raster images to sds.
  119.  *
  120.  * Also:
  121.  *         All code for the library was modified to conform to the
  122.  *         ANSI C standard.
  123.  *
  124.  * Revision 3.2  1990/07/02  10:11:46  clow
  125.  * some cosmetic modifications
  126.  *
  127. */
  128. #include "hdf.h"
  129. #include "hfile.h"
  130. #include "herr.h"
  131.  
  132. #ifdef MAC
  133. /* this isn't worth putting in hdfi.h */
  134. /* otherwise, need to include stdlib.h, which */
  135. /* in turn requires other includes - very messy, trust me */ 
  136. void qsort(void *base, size_t nmemb, size_t size, 
  137.         int (*compar) (const void*, const void *));
  138. #endif
  139.  
  140. #define MAXBUFF 8192
  141.  
  142. dd_t desc[MAXBUFF];
  143.  
  144. intn
  145.     debug    = FALSE,        /* Debugging is off by default */
  146.     sort     = TRUE,         /* Sorting is on by default */
  147.     longout  = FALSE,        /* short output by default */
  148.     labels   = FALSE,        /* no label info by default */
  149.     only_tag = DFTAG_NULL;   /* by default print info about all tags */
  150.  
  151. char * file_name;    /* name of current file being listed */
  152.  
  153. int compare
  154.   PROTO((dd_t *a, dd_t *));
  155. int main
  156.   PROTO((int, char **));
  157. int lprint
  158.   PROTO((dd_t *, int));
  159.  
  160. #ifdef PROTOTYPE
  161. int compare(dd_t *a, dd_t *b)
  162. #else
  163. int compare(a, b)
  164. dd_t *a,*b;
  165. #endif /* PROTOTYPE */
  166. {
  167.     if (a->tag>b->tag) return(1);
  168.     if (a->tag<b->tag) return(-1);
  169.     if (a->ref>b->ref) return(1);
  170.     if (a->ref<b->ref) return(-1);
  171.     return(0);
  172. }
  173.  
  174. #ifdef PROTOTYPE
  175. main(int argc, char *argv[])
  176. #else
  177. main(argc, argv)
  178. int argc;
  179. char *argv[];
  180. #endif /* PROTOTYPE */
  181. {
  182.     int32 fid, aid;
  183.     int i=1, j, n, status;
  184.     filerec_t *file_rec;    /* file record */
  185.     dd_t desc[MAXBUFF];
  186.     
  187.     while((i < argc) && (argv[i][0]=='-')){
  188.         switch(argv[i][1]) {
  189.         case 'o':
  190.             sort = 0;
  191.             break;
  192.         case 'd':
  193.             debug = TRUE;
  194.             break;
  195.         case 'v':
  196.             labels = TRUE;
  197.             /* fall through... */
  198.         case 'l':
  199.             longout = TRUE;
  200.             break;
  201.         case 't' :
  202.             if(argv[i][2] != '\0') 
  203.                 only_tag = atoi(&(argv[i][2]));
  204.             else 
  205.                 only_tag = atoi(&(argv[++i][0]));
  206.             break;
  207.         default:    
  208.             printf("Unknown option : -%c\n", argv[1][1]);
  209.             break;
  210.         }
  211.         i++;
  212.     }
  213.     
  214.     /*
  215.      * If a file name has not been supplied print the usage message
  216.      */
  217.     if(i == argc) {
  218.         printf("%s,  version: 1.2   date: June 8, 1992\n",argv[0]);
  219.         printf("%s [-o] [-l] [-d] [-v] [-t #] fn ....\n", argv[0]);
  220.         printf("        This program displays information about the");
  221.         printf(" data elements in\n");
  222.         printf("        HDF file.\n");
  223.         printf("    -d: offset & length info of each element in the file\n");
  224.         printf("    -o: Ordered - display in reference number order\n");
  225.         printf("    -l: Long format - display more information\n");
  226.         printf("    -v: Verbose format - display text of annotations and labels.\n");
  227.         printf("        (Verbose format automatically puts you in Long format).\n");
  228.         printf("    -t #: List only information about a specific type of tag.\n");
  229.         printf("          For example '%s -t 700 foo.hdf' \n", argv[0]);
  230.         printf("          will list information only about Scientific Data\n");
  231.         printf("          Groups.\n");
  232.         exit (1);
  233.     }
  234.     
  235.     while(i < argc) {
  236.         file_name = argv[i];
  237.         fid = Hopen(file_name, DFACC_READ, -1);
  238.         printf( "%s:  ", argv[i]);
  239.         if(fid == FAIL) {
  240.             if(HEvalue(1) == DFE_NOTDFFILE) {
  241.                 printf("\n\tNot an HDF file.\n");
  242.                 i++;
  243.                 continue;
  244.             } else {
  245.                 printf("\n");
  246.                 fflush(stdout);
  247.                 HEprint(stderr, 0);
  248.             }
  249.         }
  250.         
  251.     aid = Hstartread(fid, DFTAG_WILDCARD, DFREF_WILDCARD);
  252.     if(aid == FAIL) {
  253.             HEprint(stderr, 0);
  254.             i++;
  255.             continue;      
  256.     }
  257.         
  258.     status = SUCCEED;
  259.     for(n = 0; (n < MAXBUFF) && (status != FAIL); n++) {
  260.             Hinquire(aid, NULL, &desc[n].tag, &desc[n].ref, &desc[n].length,
  261.                      &desc[n].offset, NULL, NULL, NULL);
  262.             status = Hnextread(aid, DFTAG_WILDCARD, DFREF_WILDCARD, DF_CURRENT);
  263.     }
  264.         
  265.     if(n == MAXBUFF) {
  266.             fprintf(stderr, 
  267.                     "Warning:  File may have more DD's than hdfls can display\n");
  268.     }
  269.         
  270.     if(debug) {
  271.             printf("\n");
  272.             for (j=0; j<n; j++) {
  273.                 printf("%6d) tag %6d ref %6d ", j, desc[j].tag, desc[j].ref);
  274.                 printf(" offset %10ld length %10ld\n", desc[j].offset, desc[j].length);
  275.             }
  276.     }
  277.     
  278.     if (sort) qsort( (char *) desc, n, sizeof(dd_t), compare);
  279.     
  280.     lprint(desc, n);
  281.         
  282.     if(Hendaccess(aid) == FAIL) {
  283.             HEprint(stderr, 0);
  284.     }
  285.         
  286.         if (Hclose(fid) == FAIL) {
  287.             HEprint(stderr, 0);
  288.     }
  289.         
  290.         i++;
  291.         printf("\n");
  292.     }
  293. }
  294.  
  295. #ifdef PROTOTYPE
  296. int lprint(dd_t *desc, int num)
  297. #else
  298. int lprint(desc, num)
  299. dd_t  *desc;
  300. int num;
  301. #endif /* PROTOTYPE */
  302. {
  303.   
  304.   intn j = 0, empty = 0, status;
  305.   uint16 prev = 0;
  306.   int32 len;
  307.   char *name, *label_str;
  308.   
  309.   while (j <num) {
  310.     if (desc[j].tag == DFTAG_NULL) {
  311.       empty++;
  312.       j++;
  313.       continue;               /* don't print anything now */
  314.     }
  315.  
  316.     /* 
  317.      * skip this tag if the user only wants to see some tags and
  318.      *  this is not one of them 
  319.      */
  320.     if(only_tag != DFTAG_NULL && only_tag != desc[j].tag) {
  321.         j++;
  322.         continue;
  323.     }
  324.  
  325.     /*
  326.     ** Find and print text description of this tag
  327.     */
  328.     name = HDgettagname(desc[j].tag);
  329.     if(!name) name = "Unknown Tag";
  330.     printf("\n%-30s: (tag %d)\n", name, desc[j].tag);
  331.  
  332.     /*
  333.     ** Print out reference number information
  334.     */
  335.     prev = desc[j].tag;
  336.     if(longout) {
  337.       while (desc[j].tag == prev && j < num) {
  338.     printf("\tRef no %6d\t%8ld bytes\n", desc[j].ref, desc[j].length);
  339.         if(labels) {
  340.             /* read in all of the labels */
  341.             len = DFANgetlablen(file_name, prev, desc[j].ref);
  342.             if(len != FAIL) {
  343.                 label_str = (char *) HDgetspace((uint32) len + 1);
  344.                 status = DFANgetlabel(file_name, prev, desc[j].ref, label_str, len + 1);
  345.                 label_str[len] = '\0';
  346.                 if(status == FAIL) 
  347.                     printf("\t  Unable to read label\n");
  348.                 else
  349.                     printf("\t  Label: %s\n", label_str);
  350.                 HDfreespace(label_str);
  351.             }
  352.  
  353.             /* read in all of the annotations */
  354.             len = DFANgetdesclen(file_name, prev, desc[j].ref);
  355.             if(len != FAIL) {
  356.                 label_str = (char *) HDgetspace((uint32) len + 1);
  357.                 status = DFANgetdesc(file_name, prev, desc[j].ref, label_str, len + 1);
  358.                 label_str[len] = '\0';
  359.                 if(status == FAIL) 
  360.                     printf("\t  Unable to read description\n");
  361.                 else
  362.                     printf("\t  Description: %s\n", label_str);
  363.                 HDfreespace(label_str);
  364.             }
  365.         }
  366.     j++;
  367.       }
  368.     } else {
  369.       printf("\tRef nos:");
  370.       while (desc[j].tag==(uint16)prev && j<num) {
  371.     printf(" %d",desc[j].ref);
  372.     j++;
  373.       }
  374.     }
  375.   }
  376.   if(empty) printf("\nEmpty (tag %d): %d slots\n", DFTAG_NULL, empty);
  377.   return 0;
  378.  
  379. }
  380.